Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Array → Copy Arrays

Python Array

Copy Arrays

Copying Arrays (Lists) in Python

In Python, arrays are typically represented using lists. Copying a list isn't as straightforward as it might seem, as Python's assignment operator (`=`) creates a *shallow copy*. Understanding the difference between shallow and deep copies is crucial to avoid unexpected behavior.

1. Shallow Copy

A shallow copy creates a new list object, but it doesn't duplicate the objects within the original list. Instead, it creates new references to the *same* objects. This means if you modify an element in the copied list, the original list will also be affected, and vice-versa. Example 1: Illustrating Shallow Copy
Shallow Copy of array in python original_list = [[1, 2], [3, 4]] shallow_copy = original_list # Assignment creates a shallow copy shallow_copy[0][0] = 10 # Modifying an element in the shallow copy print("Original list:", original_list) # Output: [[10, 2], [3, 4]] print("Shallow copy:", shallow_copy) # Output: [[10, 2], [3, 4]]

Output

Original list: [[10, 2], [3, 4]] Shallow copy: [[10, 2], [3, 4]]
Notice how changing an element within the nested list in `shallow_copy` also changes the corresponding element in `original_list`. This is because both lists point to the same inner lists.
Example 2: Shallow Copy using `[:]` Slicing Slicing creates a shallow copy. While it looks different, it behaves identically to simple assignment in the context of nested lists.
Copy array using slicing in python original_list = [[1, 2], [3, 4]] shallow_copy = original_list[:] shallow_copy[1][1] = 40 print("Original list:", original_list) # Output: [[1, 2], [3, 40]] print("Shallow copy:", shallow_copy) # Output: [[1, 2], [3, 40]]

Output

Original list: [[1, 2], [3, 40]] Shallow copy: [[1, 2], [3, 40]]

2. Deep Copy

A deep copy creates a completely independent copy of the list and all its nested objects. Modifying the copied list won't affect the original, and vice-versa. Example 1: Deep Copy using the `copy` module The `copy` module provides the `deepcopy()` function for creating deep copies.
deepcopy() in python import copy original_list = [[1, 2], [3, 4]] deep_copy = copy.deepcopy(original_list) deep_copy[0][0] = 100 print("Original list:", original_list) # Output: [[1, 2], [3, 4]] print("Deep copy:", deep_copy) # Output: [[100, 2], [3, 4]]

Output

Original list: [[1, 2], [3, 4]] Deep copy: [[100, 2], [3, 4]]
As you can see, changes to `deep_copy` don't affect `original_list`.
Example 2: List Comprehension for Deep Copy (with simple lists) For simple lists (without nested lists), list comprehension offers a concise way to create a deep copy:
List Comprehension for Deep Copy original_list = [1, 2, 3, 4] deep_copy = [x for x in original_list] deep_copy[0] = 100 print("Original list:", original_list) # Output: [1, 2, 3, 4] print("Deep copy:", deep_copy) # Output: [100, 2, 3, 4]

Output

Original list: [1, 2, 3, 4] Deep copy: [100, 2, 3, 4]
Important Note `deepcopy()` handles nested structures recursively. If your list contains complex objects, `deepcopy()` ensures that all nested objects are also duplicated independently. However, it's computationally more expensive than a shallow copy.
Choosing the Right Copy Method * Use a shallow copy (`[:]` or simple assignment) when you need a new reference to the same data, and modifying the copy is acceptable for the original data as well. This is efficient but can lead to unexpected side effects. * Use a deep copy (`copy.deepcopy()`) when you need a completely independent copy of the data. This is safer but consumes more memory and time. Use it when modifications to the copy should not impact the original data. By understanding the nuances of shallow and deep copies, you can write more robust and predictable Python code that handles array manipulation effectively. Remember to choose the copying method that best suits your specific needs.

Tutorials